package com.wutuo.school.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.util.CommonUtil;
import com.wutuo.school.api.SchoolAPIService;
import com.wutuo.school.api.request.ListSchoolRequest;
import com.wutuo.school.api.request.SchoolRequest;
import com.wutuo.school.api.response.SchoolResponse;
import com.wutuo.school.provider.dao.SchoolMapper;
import com.wutuo.school.provider.domain.School;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 校区 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service public class SchoolService extends BaseService<School, Long> implements SchoolAPIService {

    private Logger logger = LoggerFactory.getLogger(SchoolService.class);

    @Autowired private SchoolMapper schoolMapper;

    @Override protected GenericMapper<School, Long> getMapper() {

        // TODO Auto-generated method stub
        return schoolMapper;
    }

    public void save(SchoolRequest record) {

        if (record.getId() == null) {
            int insert = getMapper().insert(record);
            System.out.println(insert);
        } else {
            int update = super.update(record);
            System.out.println(update);
        }
    }

    public void addorUpdate(SchoolRequest record) {

        if (record.getId() == null) {
            if (list(record).size() == 0)
                super.add(record);
        } else
            super.update(record);
    }

    public void del(Long id) {

        SchoolRequest model = new SchoolRequest();
        model.setId(id);
        super.delete(model);
    }

    public List<SchoolResponse> lisForPage(ListSchoolRequest request) {

        List<SchoolResponse> list = schoolMapper.lisForPage(request);
        return list;
    }

    public List<SchoolResponse> list(SchoolRequest request) {

        request.setDelFlag(true);
        return schoolMapper.list(request);
    }

    public School getSchoolByCreateBy(String id) {

        SchoolRequest request = new SchoolRequest();
        request.setDelFlag(true);
        request.setRemarks(id);
        List list = schoolMapper.list(request);
        if (list.size() == 0)
            return null;
        else
            return (School) list.get(0);
    }

    /**
     * 翻译school
     *
     * @param model
     * @param tKey
     * @param skey
     * @return
     */
    public <T> List getSchoolByIds(List<T> model, String tKey, String skey) {

        String schoolids = CommonUtil.getLongString(model, tKey).toString();
        if (StringUtils.isEmpty(schoolids))
            return model;
        SchoolRequest sc = new SchoolRequest();
        String scooild = schoolids.replaceAll("'", "");
        sc.setIds(scooild);
        List<SchoolResponse> listTag = list(sc);
        Map map = CommonUtil.listToObject(listTag, "id");
        for (int i = 0; i < model.size(); i++) {
            Object modelS = model.get(i);
            Object o = CommonUtil.getProperty(modelS, tKey);
            if (o == null) {
                continue;
            }

            String id = o.toString();
            String[] ids = id.split(",");
            List<T> s = new ArrayList();
            for (int j = 0; j < ids.length; j++) {
                if (map.containsKey(ids[j])) {
                    s.add((T) map.get(ids[j]));
                }
            }
            CommonUtil.setProperty(modelS, skey, s);
        }
        return model;
    }
}